﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsWorks
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Users";
            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选择整行
            dataGridView1.AllowUserToAddRows = false;//去除空白行 
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox11.Text;

            var sql = string.Format("select * from Users where Username like '%{0}%'",name);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;


        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditiForm editiForm = new EditiForm();
            var res = editiForm.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var name = textBox11.Text;
                var sqlUsr =
                    string.Format("select * from Users where UserName like '%{0}%'", name);
                var dt = DbHelper.GetDataTable(sqlUsr);

                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("添加失败", "提示");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Username"].Value;
                var pwd = (string)dataGridView1.SelectedRows[0].Cells["Password"].Value;


                EditiForm editiForm = new EditiForm(id,name,pwd);
                var res = editiForm.ShowDialog();

                
                if (res == DialogResult.Yes)
                {
                    var nameTemp = textBox11.Text;

                    var sqlUsr =
                        string.Format("select * from Users where UserName like '%{0}%'", name);
                    var dt = DbHelper.GetDataTable(sqlUsr);

                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("更新失败", "提示");
                }
            }
            else
            {
                MessageBox.Show("请选择需操作的行", "提示");
            }
        }
         


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;

                var res =
                    MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Users where Id={0}", id);
                    var rowCount = DbHelper.AddOrEditSave(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox11.Text;
                        var sqlUsr = 
                            string.Format("select * from Users where UserName like '%{0}%'",name);
                        var dt = DbHelper.GetDataTable(sqlUsr);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
                else
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
            else
            {
                MessageBox.Show("请选择所需行进行操作", "提示");
            }

        }
    }
}
